package com.xbb.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xbb.entity.Notice;
import com.xbb.entity.Qingjia;
import com.xbb.service.INoticeService;
import com.xbb.util.Result;
import com.xbb.util.Result1;
import org.aopalliance.aop.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Mr xie
 * @since 2020-06-16
 */
@RestController
@RequestMapping("/notice")
public class NoticeController {
    @Autowired
    INoticeService service;
    //管理员显示所有通告，其他用户显示要显示的通告

    /**
     * 获取并显示要显示的通告
     * @return
     */
    @RequestMapping("/getNotice")
    public Object getNotice() {
        List<Notice> list = service.list(new QueryWrapper<Notice>().eq("is_show","是").orderByDesc("create_time"));
        return list;
    }

    /**
     * 获取所有的通告
     * @param notice
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/getNoticeList")
    public Object getNoticeList(Notice notice,
                                 @RequestParam(required = false, defaultValue = "1") Integer page,
                                 @RequestParam(required = false, defaultValue = "10") Integer limit) {
        Page<Notice> page1 = new  Page<Notice>(page,limit);
        IPage<Notice> iPage = service.page(page1);
        List<Notice> data = iPage.getRecords();
        long total = iPage.getTotal();
        return new Result(0,"公告列表",total,data);
    }

    /**
     * 添加公告
     * @param notice
     * @return
     */
    @RequestMapping("/addNotice")
    public Object addNotice(Notice notice) {
        notice.setIsShow("是");
        notice.setCreateTime(new Date());
        boolean save = service.save(notice);
        if(save) {
            return new Result1(0, "添加公告成功");
        }
        return new Result1(1, "添加公告失败");
    }

    /**
     * 修改公告是否显示
     * @param id
     * @param isshow
     * @return
     */
    @RequestMapping("/updateNotice")
    public Object updateNotice(Integer id,String isshow) {
        Notice notice=new Notice();
        notice.setId(id);
        notice.setIsShow(isshow);
        boolean save = service.updateById(notice);
        if(save) {
            return new Result1(0, "修改公告成功");
        }
        return new Result1(1, "修改公告失败");
    }

    /**
     * 修改公告内容信息
     * @param notice
     * @return
     */
    @RequestMapping("/updateNotice2")
    public Object updateNotice2(Notice notice) {
        System.out.println(notice);
        //notice.setId(id);
        boolean save = service.updateById(notice);
        if(save) {
            return new Result1(0, "修改公告成功");
        }
        return new Result1(1, "修改公告失败");
    }
}
